﻿exports.keypress = function (req, res) {
    var RESULT_SUCESS = 0;
    var RESULT_ERROR = -1;
    var fs = require("fs");
    var pconfig = require("../common/config").config;
    var DateUtils = require("../common/dateUtils").DateUtils;
    var now = new Date();
    var d = DateUtils.dateToString(now);
    var suffix = DateUtils.dateToString(now, "yyyy-MM-dd");
    var fullPath = pconfig.filePathPrefix + pconfig.keyFileName + "." + suffix;
    if (req.method == "POST") {
        var datas = req.body;
        var isSuccess = true;
        
        fs.appendFile(fullPath, '[' + d + '][' + datas.guid + ']' + JSON.stringify(datas) + '\n', function (err) {
            if (err) {
                isSuccess = false;
                console.log(err);
            }
        });    
    }
    var result = {};
    if (isSuccess) {
        result = { code: RESULT_SUCESS, interval: 60000, message: null };
    } else {
        result = { code: RESULT_ERROR, interval: 60000, message: "save file error." };
    }
    res.send(JSON.stringify(result));
};